﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
					xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
					xmlns:b="using:CalendarWindowsApp.Behaviors"
					xmlns:c="using:CalendarWindowsApp.Controls"
					xmlns:t="using:CalendarWindowsApp.Tools"
					xmlns:Interactivity="using:Microsoft.Xaml.Interactivity"
					xmlns:k="using:Microsoft.Xaml.Interactions"
					xmlns:Core="using:Microsoft.Xaml.Interactions.Core"
					xmlns:calendar="using:CalendarWindowsApp.Controls.Calendar"
					xmlns:local="using:CalendarWindowsApp">
	<Style TargetType="calendar:BaseCalendar">
		<Setter Property="Template">
			<Setter.Value>
				<ControlTemplate TargetType="calendar:BaseCalendar">
					<Border x:Name="ControlBorder"
							Background="{StaticResource Calendar|BackgroundBrush}"
							BorderBrush="{TemplateBinding BorderBrush}"
							BorderThickness="{TemplateBinding BorderThickness}">
						<Border.Resources>
							<CollectionViewSource x:Name="CollectionViewSourceDay"
												  IsSourceGrouped="True" />
							<CollectionViewSource x:Name="CollectionViewSourceDayHeader"
												  IsSourceGrouped="False" />
							<CollectionViewSource x:Name="CollectionViewSourceWeek"
												  IsSourceGrouped="True" />
							<CollectionViewSource x:Name="CollectionViewSourceWeekHeader"
												  IsSourceGrouped="False" />
							<CollectionViewSource x:Name="CollectionViewSourceMonth"
												  IsSourceGrouped="True" />
							<CollectionViewSource x:Name="FlyoutCollectionSource" />
						</Border.Resources>
						<Page>
							<Page.TopAppBar>
								<AppBar x:Name="calendarAppBar">
									<Grid>
										<StackPanel Orientation="Horizontal">
											<AppBarButton Label="Month"
														  Icon="Calendar">
												<Interactivity:Interaction.Behaviors>
													<Core:EventTriggerBehavior EventName="Click">
														<Core:ChangePropertyAction PropertyName="CenteredIndex"
																				   TargetObject="{Binding ElementName=PanoramaPanel}"
																				   Value="0" />
														<Core:ChangePropertyAction PropertyName="IsOpen"
																				   TargetObject="{Binding ElementName=calendarAppBar}"
																				   Value="False" />
													</Core:EventTriggerBehavior>
												</Interactivity:Interaction.Behaviors>
											</AppBarButton>
											<AppBarButton Label="Week"
														  Icon="CalendarWeek">
												<Interactivity:Interaction.Behaviors>
													<Core:EventTriggerBehavior EventName="Click">
														<Core:ChangePropertyAction PropertyName="CenteredIndex"
																				   TargetObject="{Binding ElementName=PanoramaPanel}"
																				   Value="1" />
														<Core:ChangePropertyAction PropertyName="IsOpen"
																				   TargetObject="{Binding ElementName=calendarAppBar}"
																				   Value="False" />
													</Core:EventTriggerBehavior>
												</Interactivity:Interaction.Behaviors>
											</AppBarButton>
											<AppBarButton Label="Day"
														  Icon="CalendarDay">
												<Interactivity:Interaction.Behaviors>
													<Core:EventTriggerBehavior EventName="Click">
														<Core:ChangePropertyAction PropertyName="CenteredIndex"
																				   TargetObject="{Binding ElementName=PanoramaPanel}"
																				   Value="2" />
														<Core:ChangePropertyAction PropertyName="IsOpen"
																				   TargetObject="{Binding ElementName=calendarAppBar}"
																				   Value="False" />
													</Core:EventTriggerBehavior>
												</Interactivity:Interaction.Behaviors>
											</AppBarButton>
										</StackPanel>
									</Grid>
								</AppBar>
							</Page.TopAppBar>
							<Grid>
								<Grid.RowDefinitions>
									<RowDefinition Height="Auto"
												   MinHeight="100" />
									<RowDefinition Height="*" />
								</Grid.RowDefinitions>
								<Grid x:Name="Title">
									<Grid.ColumnDefinitions>
										<ColumnDefinition x:Name="BackwardsButtonColumn"
														  Width="50" />
										<ColumnDefinition x:Name="CalendarHeaderColumn"
														  Width="*" />
										<ColumnDefinition x:Name="ForwardButtonColumn"
														  Width="50" />
									</Grid.ColumnDefinitions>
									<Button x:Name="previousButton"
											Style="{StaticResource IconButtonStyle}"
											Grid.Column="0">
										<Button.Content>
											<Path Stretch="Fill"
												  Width="14"
												  Height="20"
												  Fill="{Binding ElementName=previousButton, Path=Foreground}"
												  Data="F1 M 11.4792,15.8334L 27.3125,31.6667L 16.625,31.6667L 0,15.8333L 16.625,1.90735e-006L 27.3125,1.71661e-005L 11.4792,15.8334 Z " />
										</Button.Content>
									</Button>
									<Button x:Name="forwardButton"
											Style="{StaticResource IconButtonStyle}"
											Grid.Column="2">
										<Button.Content>
											<Path Stretch="Fill"
												  Width="14"
												  Height="20"
												  Fill="{Binding ElementName=forwardButton, Path=Foreground}"
												  Data="F1 M 15.8333,15.8333L 0,31.6667L 10.6875,31.6667L 27.3125,15.8333L 10.6875,0L 0,1.14441e-005L 15.8333,15.8333 Z " />
										</Button.Content>
									</Button>
									<StackPanel x:Name="TitleStack"
												Orientation="Horizontal"
												Grid.Column="1">
										<TextBlock x:Name="titleTextBlock"
												   VerticalAlignment="Center"
												   FontSize="36"
												   Foreground="{StaticResource Calendar|DayTitle|ForegroundBrush}"
												   Grid.Column="1" />
										<Button x:Name="FlyoutButton"
												VerticalAlignment="Center"
												BorderBrush="{x:Null}">
											<Button.Content>
												<Path Stretch="Fill"
													  Width="14"
													  Height="20"
													  Fill="{Binding ElementName=titleTextBlock, Path=Foreground}"
													  RenderTransformOrigin="0.5,0.5"
													  Data="F1 M 11.4792,15.8334L 27.3125,31.6667L 16.625,31.6667L 0,15.8333L 16.625,1.90735e-006L 27.3125,1.71661e-005L 11.4792,15.8334 Z ">
													<Path.RenderTransform>
														<CompositeTransform Rotation="270" />
													</Path.RenderTransform>
												</Path>
											</Button.Content>
											<Button.Flyout>
												<Flyout x:Name="calendarFlyout"
														Placement="Bottom">
													<Flyout.Content>
														<ItemsControl ItemsSource="{Binding Source={StaticResource FlyoutCollectionSource}}">
															
														</ItemsControl>
													</Flyout.Content>
															<Flyout.FlyoutPresenterStyle>
																<Style TargetType="FlyoutPresenter">
																	<Setter Property="ScrollViewer.ZoomMode"
																			Value="Enabled" />
																	<Setter Property="Background"
																			Value="White" />
																	<Setter Property="BorderBrush"
																			Value="{x:Null}" />
																	<Setter Property="MinHeight"
																			Value="350" />
																	<Setter Property="MinWidth"
																			Value="400" />
																	<Setter Property="Margin"
																			Value="32,-2,0,0" />
																</Style>
															</Flyout.FlyoutPresenterStyle>
												</Flyout>
											</Button.Flyout>
										</Button>
									</StackPanel>
								</Grid>
								<c:PanoramaPanel x:Name="PanoramaPanel"
												 CenteredIndex="0"
												 Grid.Row="1">
									<Grid x:Name="MonthViewGrid">
										<GridView x:Name="GridViewMonth"
												  ItemsSource="{Binding Source={StaticResource CollectionViewSourceMonth}}">
											<GridView.ItemTemplate>
												<DataTemplate>
													<Grid x:Name="monthViewItemTemplateGrid">
														<Interactivity:Interaction.Behaviors>
															<b:StretchToParentBehavior />
															<Core:EventTriggerBehavior EventName="Tapped">
																<Core:ChangePropertyAction PropertyName="IsSelected"
																						   TargetObject="{Binding}"
																						   Value="True" />
															</Core:EventTriggerBehavior>
															<Core:DataTriggerBehavior Binding="{Binding IsWeekEnd}"
																					  Value="True">
																<Core:ChangePropertyAction PropertyName="Background"
																						   Value="{StaticResource Calendar|WeekendDayBackground}" />
															</Core:DataTriggerBehavior>
														</Interactivity:Interaction.Behaviors>
														<TextBlock Text="{Binding Date.Day}"
																   Margin="10"
																   Foreground="{StaticResource Calendar|Day|ForegroundBrush}"
																   Style="{StaticResource BodyTextBlockStyle}" />
														<ItemsControl ItemsSource="{Binding AllDayAppointments}"
																	  ItemTemplate="{StaticResource DataTemplate|AllDayAppointments}"
																	  VerticalAlignment="Center"
																	  Grid.Row="1" />
														<Popup x:Name="AddAppointmentPopup"
															   HorizontalAlignment="Center"
															   VerticalAlignment="Top"
															   IsOpen="{Binding IsSelected}">
															<Grid Background="White">
																<Grid.ColumnDefinitions>
																	<ColumnDefinition Width="8" />
																	<ColumnDefinition Width="*" />
																</Grid.ColumnDefinitions>
																<Rectangle Fill="LightGreen"
																		   Height="40"
																		   HorizontalAlignment="Stretch" />
																<TextBox PlaceholderText="Subject"
																		 Margin="10,0"
																		 Grid.Column="1">
																	<Interactivity:Interaction.Behaviors>
																		<b:AddAppointmentBehavior />
																	</Interactivity:Interaction.Behaviors>
																</TextBox>
															</Grid>
														</Popup>
													</Grid>
												</DataTemplate>
											</GridView.ItemTemplate>
											<GridView.ItemsPanel>
												<ItemsPanelTemplate>
													<ItemsWrapGrid x:Name="GridViewItemsWrapGrid">
														<Interactivity:Interaction.Behaviors>
															<b:DynamicPanelSizeBehavior />
														</Interactivity:Interaction.Behaviors>
													</ItemsWrapGrid>
												</ItemsPanelTemplate>
											</GridView.ItemsPanel>
											<GridView.GroupStyle>
												<GroupStyle>
													<GroupStyle.HeaderTemplate>
														<DataTemplate>
															<Grid>
																<TextBlock Text='{Binding Key}'
																		   Foreground="{StaticResource Calendar|DayTitle|ForegroundBrush}"
																		   Style="{StaticResource BaseTextBlockStyle}" />
															</Grid>
														</DataTemplate>
													</GroupStyle.HeaderTemplate>
												</GroupStyle>
											</GridView.GroupStyle>
										</GridView>
									</Grid>
									<Grid x:Name="WeekViewGrid">
										<Grid>
											<Grid.RowDefinitions>
												<RowDefinition Height="Auto" />
												<RowDefinition Height="*" />
											</Grid.RowDefinitions>
											<Grid.ColumnDefinitions>
												<ColumnDefinition Width="61" />
												<ColumnDefinition Width="*" />
											</Grid.ColumnDefinitions>
											<Border x:Name="WeekHeaderBorder"
													Grid.Row="0"
													Grid.Column="1">
												<ItemsControl Height="80"
															  ItemsSource="{Binding Source={StaticResource CollectionViewSourceWeekHeader}}"
															  ItemsPanel="{StaticResource ItemsControlDynamicTemplate}">
													<ItemsControl.ItemTemplate>
														<DataTemplate>
															<Grid Height="80">
																<Grid.RowDefinitions>
																	<RowDefinition Height="50" />
																	<RowDefinition Height="30" />
																</Grid.RowDefinitions>
																<Border BorderBrush="White"
																		BorderThickness="1,0,0,0">
																	<TextBlock Text="{Binding Title}"
																			   Margin="10"
																			   Foreground="{StaticResource Calendar|DayTitle|ForegroundBrush}"
																			   Style="{StaticResource BaseTextBlockStyle}" />
																</Border>
																<Border BorderBrush="White"
																		Grid.Row="1"
																		BorderThickness="1,1,0,0">
																	<ItemsControl ItemsSource="{Binding AllDayAppointments}"
																				  ItemsPanel="{StaticResource ItemsControlDynamicTemplate}"
																				  ItemTemplate="{StaticResource DataTemplate|AllDayAppointments}" />
																</Border>
															</Grid>
														</DataTemplate>
													</ItemsControl.ItemTemplate>
												</ItemsControl>
											</Border>
											<Border x:Name="ListViewBorder"
													BorderThickness="0,1,0,0"
													BorderBrush="White"
													Grid.Row="1"
													Grid.Column="0"
													Grid.ColumnSpan="2">
												<ListView ItemsSource="{Binding Source={StaticResource CollectionViewSourceWeek}}">
													<ListView.ItemTemplate>
														<DataTemplate>
															<Grid>
																<Interactivity:Interaction.Behaviors>
																	<b:StretchToParentBehavior />
																	<Core:EventTriggerBehavior EventName="Tapped">
																		<Core:ChangePropertyAction PropertyName="IsSelected"
																								   TargetObject="{Binding}"
																								   Value="True" />
																	</Core:EventTriggerBehavior>
																</Interactivity:Interaction.Behaviors>
															</Grid>
														</DataTemplate>
													</ListView.ItemTemplate>
													<ListView.GroupStyle>
														<GroupStyle>
															<GroupStyle.ContainerStyle>
																<Style TargetType="GroupItem">
																	<Setter Property="Height"
																			Value="60" />
																	<Setter Property="BorderBrush"
																			Value="White" />
																	<Setter Property="BorderThickness"
																			Value="1,1,0,0" />
																	<Setter Property="Template">
																		<Setter.Value>
																			<ControlTemplate TargetType="GroupItem">
																				<Border Background="{TemplateBinding Background}"
																						BorderBrush="{TemplateBinding BorderBrush}"
																						BorderThickness="{TemplateBinding BorderThickness}">
																					<Grid>
																						<Grid.ColumnDefinitions>
																							<ColumnDefinition Width="60" />
																							<ColumnDefinition Width="*" />
																						</Grid.ColumnDefinitions>
																						<ContentControl x:Name="HeaderContent"
																										Content="{TemplateBinding Content}"
																										HorizontalAlignment="Center"
																										VerticalAlignment="Center"
																										ContentTransitions="{TemplateBinding ContentTransitions}"
																										ContentTemplate="{TemplateBinding ContentTemplate}"
																										ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"
																										Margin="{TemplateBinding Padding}"
																										TabIndex="0"
																										IsTabStop="False" />
																						<ItemsControl Grid.Column="1"
																									  ItemsSource="{Binding GroupItems}"
																									  ItemsPanel="{StaticResource ItemsControlDynamicTemplate}"
																									  IsTabStop="False"
																									  TabNavigation="Once"
																									  TabIndex="1">
																							<ItemsControl.ItemContainerTransitions>
																								<TransitionCollection>
																									<AddDeleteThemeTransition />
																									<ContentThemeTransition />
																									<ReorderThemeTransition />
																									<EntranceThemeTransition IsStaggeringEnabled="False" />
																								</TransitionCollection>
																							</ItemsControl.ItemContainerTransitions>
																						</ItemsControl>
																					</Grid>
																				</Border>
																			</ControlTemplate>
																		</Setter.Value>
																	</Setter>
																</Style>
															</GroupStyle.ContainerStyle>
															<GroupStyle.HeaderTemplate>
																<DataTemplate>
																	<Grid>
																		<TextBlock Text='{Binding Key}'
																				   Foreground="{StaticResource Calendar|Day|ForegroundBrush}"
																				   Style="{StaticResource BaseTextBlockStyle}" />
																	</Grid>
																</DataTemplate>
															</GroupStyle.HeaderTemplate>
														</GroupStyle>
													</ListView.GroupStyle>
												</ListView>
											</Border>
										</Grid>
									</Grid>
									<Grid x:Name="DayViewGrid">
										<Grid>
											<Grid.RowDefinitions>
												<RowDefinition Height="Auto" />
												<RowDefinition Height="*" />
											</Grid.RowDefinitions>
											<Border x:Name="dayHeaderBorder"
													Grid.Column="1">
												<ItemsControl Height="80"
															  ItemsSource="{Binding Source={StaticResource CollectionViewSourceDayHeader}}"
															  ItemsPanel="{StaticResource ItemsControlDynamicTemplate}">
													<ItemsControl.ItemTemplate>
														<DataTemplate>
															<Grid Height="80">
																<Grid.ColumnDefinitions>
																	<ColumnDefinition Width="39" />
																	<ColumnDefinition Width="*" />
																</Grid.ColumnDefinitions>
																<Grid.RowDefinitions>
																	<RowDefinition Height="50" />
																	<RowDefinition Height="30" />
																</Grid.RowDefinitions>
																<Border BorderBrush="White"
																		BorderThickness="1,0,0,0"
																		Grid.RowSpan="2" />
																<Border BorderBrush="White"
																		Grid.Column="1"
																		BorderThickness="1,0,0,0">
																	<TextBlock Text="{Binding}"
																			   Margin="10"
																			   Foreground="{StaticResource Calendar|DayTitle|ForegroundBrush}"
																			   Style="{StaticResource BaseTextBlockStyle}" />
																</Border>
																<Border BorderBrush="White"
																		Grid.Row="1"
																		Grid.Column="1"
																		BorderThickness="1,1,0,0">
																	<!--TODO: The full day's appointments-->
																</Border>
															</Grid>
														</DataTemplate>
													</ItemsControl.ItemTemplate>
												</ItemsControl>
											</Border>
											<Border x:Name="dayItemsControl"
													BorderThickness="0,1,0,0"
													BorderBrush="White"
													Grid.Row="1">
												<ItemsControl ItemsSource="{Binding Source={StaticResource CollectionViewSourceDay}}">
													<ItemsControl.ItemsPanel>
														<ItemsPanelTemplate>
															<c:DynamicPanel />
														</ItemsPanelTemplate>
													</ItemsControl.ItemsPanel>
													<ItemsControl.ItemTemplate>
														<DataTemplate>
															<ItemsControl ItemsSource="{Binding Items}">
																<ItemsControl.Template>
																	<ControlTemplate TargetType="ItemsControl">
																		<ScrollViewer HorizontalScrollBarVisibility="Disabled"
																					  VerticalScrollBarVisibility="Auto"
																					  VerticalScrollMode="Enabled">
																			<ItemsPresenter />
																		</ScrollViewer>
																	</ControlTemplate>
																</ItemsControl.Template>
																<ItemsControl.ItemTemplate>
																	<DataTemplate>
																		<Border BorderBrush="White"
																				BorderThickness="0,0,1,1">
																			<Grid Height="60">
																				<Grid.ColumnDefinitions>
																					<ColumnDefinition Width="40" />
																					<ColumnDefinition Width="*" />
																				</Grid.ColumnDefinitions>
																				<Border BorderBrush="White"
																						BorderThickness="0,0,1,0">
																					<TextBlock Text="{Binding hour}"
																							   Margin="10"
																							   Foreground="{StaticResource Calendar|Day|ForegroundBrush}"
																							   Style="{StaticResource BaseTextBlockStyle}" />
																				</Border>
																			</Grid>
																		</Border>
																	</DataTemplate>
																</ItemsControl.ItemTemplate>
															</ItemsControl>
														</DataTemplate>
													</ItemsControl.ItemTemplate>
												</ItemsControl>
											</Border>
										</Grid>
									</Grid>
								</c:PanoramaPanel>
							</Grid>
						</Page>
					</Border>
				</ControlTemplate>
			</Setter.Value>
		</Setter>
	</Style>
	<DataTemplate x:Key="DataTemplate|AllDayAppointments">
		<Grid Background="White">
			<Grid.ColumnDefinitions>
				<ColumnDefinition Width="8" />
				<ColumnDefinition Width="*" />
			</Grid.ColumnDefinitions>
			<Rectangle Fill="LightGreen"
					   Stretch="Fill"
					   HorizontalAlignment="Stretch" />
			<TextBlock Text="{Binding Subject}"
					   FontSize="16"
					   Foreground="{StaticResource Calendar|DayTitle|ForegroundBrush}"
					   Margin="10,0,0,0"
					   Grid.Column="1" />
		</Grid>
	</DataTemplate>
</ResourceDictionary>